import { useIntl } from 'umi';
import useAuthButton from './useAuthButton';

const useIntlCustom = () => {
  const intl = useIntl();
  const { hasPermission } = useAuthButton();

  /**
   *
   * @param prefix 前缀
   * @param key 字段id
   * @param values 动态拼接的字段
   * @returns
   */
  const translate = (prefix: string, key: string, params?: Record<string, any>): string => {
    const id = `${prefix}.${key}`;
    let translatedText = intl.formatMessage({ id }) || '';
    // 如果有参数，进行模板替换
    if (params) {
      Object.entries(params).forEach(([key, value]) => {
        translatedText = translatedText.replace(`{${key}}`, String(value));
      });
    }

    return translatedText;
  };

  const defaultInputPlaceholder = translate('common', 'inputPlaceholder');
  const defaultSelectPlaceholder = translate('common', 'selectPlaceholder');

  return {
    translate,
    defaultInputPlaceholder,
    defaultSelectPlaceholder,
  };
};

export default useIntlCustom;
